export default {
  // 命名空间
  namespaced: true,

  state: {
    raw: [{ id: 1, name: "Apple", count: 1, price: 10 }],
  },

  getters: {
    countTotal(state) {
      return state.raw.reduce((total, item) => {
        return (total += item.count);
      }, 0);
    },
    msg() {
      return "cart";
    },
  },

  mutations: {
    addProduct(state) {
      state.raw.push({ id: 2, name: "Banana", count: 2, price: 20 });
    },
    add(state) {
      state.raw[0].count++;
    },
    increment(state) {
      state.raw[0].count++;
    },
  },

  actions: {
    addAsync({ commit }) {
      setTimeout(() => {
        commit("add");
      }, 1000);
    },
    incrementAsync({ commit }) {
      setTimeout(() => {
        commit("add");
      }, 1000);
    },
  },
};
